#ifndef BQ76930_H
#define BQ76930_H


#include "stm32f10x.h"

#include <stdint.h>
#include <stdbool.h>

/*====================================*/
/* -- for gpio -----------------------*/
#define BQ7693x_WAKE_RCC               RCC_APB2Periph_GPIOA
#define BQ7693x_WAKE_GPIOA_PORT        GPIOA
#define BQ7693x_WAKE_PIN               GPIO_Pin_8
#define MCU_WAKE_BQ7693x_ONOFF(x)      GPIO_WriteBit(BQ7693x_WAKE_GPIOA_PORT ,BQ7693x_WAKE_PIN,x)

#define BQ7693x_ALERT_RCC               RCC_APB2Periph_GPIOB
#define BQ7693x_ALERT_GPIOA_PORT        GPIOB
#define BQ7693x_ALERT_PIN               GPIO_Pin_7
#define MCU_ALERT_BQ7693x_READ()       GPIO_ReadOutputDataBit(BQ7693x_ALERT_GPIOA_PORT ,BQ7693x_ALERT_PIN)

/*====================================*/
/* -- register -----------------------*/
#define SYS_STAT 0x00
#define CELLBAL1 0x01
#define CELLBAL2 0x02
#define SYS_CTRL1 0x04
#define SYS_CTRL2 0x05
#define PROTECT1 0x06
#define PROTECT2 0x07
#define PROTECT3 0x08
#define OV_TRIP 0x09
#define UV_TRIP 0x0A
#define CC_CFG  0x0B

#define OVPThreshold     4200  // 3150-4700
#define OV_Re_Threshold  4150

#define UVPThreshold  2800 //  1580-3100
#define UV_Re_Threshold  3000

#define OTPThreshold  60 //  
#define UTPThreshold  -20 //
#define TempPThreshold -50

#define VC1_HI_BYTE 0x0C
#define VC1_LO_BYTE 0x0D

#define VC2_HI_BYTE 0x0E
#define VC2_LO_BYTE 0x0F

#define VC3_HI_BYTE 0x10
#define VC3_LO_BYTE 0x11

#define VC4_HI_BYTE 0x12
#define VC4_LO_BYTE 0x13

#define VC5_HI_BYTE 0x14
#define VC5_LO_BYTE 0x15

#define VC6_HI_BYTE 0x16
#define VC6_LO_BYTE 0x17

#define VC7_HI_BYTE 0x18
#define VC7_LO_BYTE 0x19

#define VC8_HI_BYTE 0x1A
#define VC8_LO_BYTE 0x1B

#define VC9_HI_BYTE 0x1C
#define VC9_LO_BYTE 0x1D

#define VC10_HI_BYTE 0x1E
#define VC10_LO_BYTE 0x1F

#define BAT_HI_BYTE 0x2A
#define BAT_LO_BYTE 0x2B

#define TS1_HI_BYTE 0x2C
#define TS1_LO_BYTE 0x2D

#define TS2_HI_BYTE 0x2E
#define TS2_LO_BYTE 0x2F

#define CC_HI_BYTE 0x32
#define CC_LO_BYTE 0x33

#define ADCGAIN1 0x50
#define ADCOFFSET 0x51
#define ADCGAIN2 0x59

#define SCD_DELAY_50us		0x0
#define SCD_DELAY_100us		0x1
#define SCD_DEALY_200us		0x2
#define SCD_DELAY_400us		0x3

#define SCD_THRESH_44mV_22mV	0
#define SCD_THRESH_67mV_33mV	1
#define SCD_THRESH_89mV_44mV	2
#define SCD_THRESH_111mV_56mV	3
#define SCD_THRESH_133mV_67mV	4
#define SCD_TRHESH_155mV_68mV	5
#define SCD_THRESH_178mV_89mV	6
#define SCD_THRESH_200mV_100mV	7

#define OCD_DEALY_10ms		0x0
#define OCD_DELAY_20ms		0x1
#define OCD_DELAY_40ms		0x2
#define OCD_DELAY_80ms		0x3
#define OCD_DELAY_160ms		0x4
#define OCD_DELAY_320ms		0x5
#define OCD_DELAY_640ms		0x6
#define OCD_DELAY_1280ms	0x7

#define OCD_THRESH_17mV_8mV		0
#define OCD_THRESH_22mV_11mV	1
#define OCD_THRESH_28mV_14mV	2
#define OCD_THRESH_33mV_17mV	3
#define OCD_THRESH_39mV_19mV	4
#define OCD_THRESH_44mV_22mV	5
#define OCD_THRESH_50mV_25mV	6
#define OCD_THRESH_56mV_28MV	7
#define OCD_THRESH_61mV_31mV	8
#define OCD_THRESH_67mV_33mV	9
#define OCD_THRESH_72mV_36mV	0xA
#define OCD_THRESH_78mV_39mV	0xB
#define OCD_THRESH_83mV_42mV	0xC
#define OCD_THRESH_89mV_44mV	0xD
#define OCD_THRESH_94mV_47mV	0xE
#define OCD_THRESH_100mV_50mV	0xF

#define UV_DELAY_1s				0
#define UV_DELAY_4s				1
#define UV_DELAY_8s				2
#define UV_DELAY_16s			3

#define OV_DELAY_1s				0
#define OV_DELAY_2s				1
#define OV_DELAY_4s				2
#define OV_DELAY_8s				3

#define OV_THRESH_BASE			0x2008
#define UV_THRESH_BASE			0x1000
#define OV_STEP					0x10
#define UV_STEP					0x10

#define ADCGAIN_BASE			365

#define LOW_BYTE(Data)			(unsigned char)(0xff & Data)
#define HIGH_BYTE(Data)			(unsigned char)(0xff & (Data >> 8))
	

void bq76930_init(void);

#endif /* BQ769X0_H_ */


